//
//  SuperviseCheckEffect.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/7.
//

import Foundation
import UIKit

// 1 待检查 2 合格 3不合格 4 不涉及
enum SuperviseCheckingEnum: String {
    case none = "1"
    case pass = "2"
    case fail = "3"
    case skip = "4"
}

class SuperviseCheckEffect: UIView, UITextViewDelegate {
    var isCheckType: SuperviseCheckingEnum?
    
    let contentV = SuperviseCheckMsgView()

    let passItem = SuperviseCheckItem(withType: .pass)
    let failItem = SuperviseCheckItem(withType: .fail)
    let skipItem = SuperviseCheckItem(withType: .skip)
    
    func textViewDidEndEditing(_ textView: UITextView) {
        didSelectItemWith?(isCheckType?.rawValue ?? "1", contentV.msgTV.text, contentV.images)
    }

    init(withType type: SuperviseCheckingEnum? = nil) {
        super.init(frame: .zero)
        setupUI()
        self.isCheckType = type
        updateEntry()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open var didSelectItemWith:((_ rwzt: String, _ msg: String?, _ imgs: [UIImage]?) -> ())?
    
    func reload(withModal modal: SpRwjlMbsModal) {
        titleLabel.text = modal.rwmc
        contentV.msgTV.text = ""
        contentV.images = []
    }
    
    @objc func checkTapped(_ sender: SuperviseCheckItem) {
        isCheckType = sender.type
        didSelectItemWith?(isCheckType?.rawValue ?? "1", contentV.msgTV.text, contentV.images)
        updateEntry()
    }
    
    func updateEntry() {
        passItem.isCheck = isCheckType == passItem.type
        failItem.isCheck = isCheckType == failItem.type
        skipItem.isCheck = isCheckType == skipItem.type
        
        if isCheckType == .fail {
            addFailMsgView()
        } else {
            contentV.images = []
            removeFailMsgView()
        }
    }
    
    func addFailMsgView() {
        contentV.layer.opacity = 1.0
        contentV.snp.remakeConstraints { make in
            make.left.equalToSuperview().offset(17)
            make.right.equalToSuperview().offset(-17)
            make.top.equalTo(passItem.snp.bottom).offset(8)
            make.bottom.equalToSuperview().offset(-18)
        }
    }
    
    func removeFailMsgView() {
        contentV.layer.opacity = 0.0
        contentV.snp.remakeConstraints { make in
            make.left.equalToSuperview().offset(17)
            make.right.equalToSuperview().offset(-17)
            make.top.equalTo(passItem.snp.bottom)
            make.height.equalTo(0)
            make.bottom.equalToSuperview().offset(-18)
        }
    }
    
    func setupUI() {
        let baseV = UIView.createBase()
        addSubview(baseV)
        baseV.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.right.equalToSuperview().offset(-10)
            make.bottom.equalToSuperview()
        }
        
        baseV.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(15)
            make.left.equalToSuperview().offset(17)
            make.right.equalToSuperview().offset(-17)
        }
        
        let itemW = (ScreenWidth - 95) / 3.0
        
        passItem.addTarget(self, action: #selector(checkTapped), for: .touchUpInside)
        baseV.addSubview(passItem)
        passItem.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(15)
            make.left.equalToSuperview().offset(17)
            make.width.equalTo(itemW)
            make.height.equalTo(65)
        }
        
        failItem.addTarget(self, action: #selector(checkTapped), for: .touchUpInside)
        baseV.addSubview(failItem)
        failItem.snp.makeConstraints { make in
            make.left.equalTo(passItem.snp.right).offset(17)
            make.width.equalTo(itemW)
            make.height.equalTo(passItem.snp.height)
            make.top.equalTo(passItem.snp.top)
        }
        
        skipItem.addTarget(self, action: #selector(checkTapped), for: .touchUpInside)
        baseV.addSubview(skipItem)
        skipItem.snp.makeConstraints { make in
            make.left.equalTo(failItem.snp.right).offset(17)
            make.width.equalTo(itemW)
            make.height.equalTo(passItem.snp.height)
            make.top.equalTo(passItem.snp.top)
        }
        
        contentV.layer.opacity = 0.0
        contentV.msgTV.delegate = self
        contentV.imgPicker.didSelectImagesWith = {(imgs) -> () in
            self.didSelectItemWith?(self.isCheckType?.rawValue ?? "1", self.contentV.msgTV.text, imgs)
        }
        baseV.addSubview(contentV)
        contentV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(17)
            make.right.equalToSuperview().offset(-17)
            make.top.equalTo(passItem.snp.bottom)
            make.height.equalTo(0)
            make.bottom.equalToSuperview().offset(-18)
        }
    }
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 15)
        label.textColor = .hex("#333333")
        label.numberOfLines = 0
        return label
    }()
}

class SuperviseCheckItem: UIControl {
    var type: SuperviseCheckingEnum = .pass
    var isCheck: Bool = false {
        didSet {
            updateEntry()
        }
    }
    
    func updateEntry() {
        if isCheck {
            switch type {
            case .pass:
                titleLabel.textColor = .hex("#0F499E")
                remindLabel.textColor = .hex("#0F499E")
                layer.borderColor = UIColor.hex("#0F499E").cgColor
                break
            case .fail:
                titleLabel.textColor = .hex("#FF0000")
                remindLabel.textColor = .hex("#FF0000")
                layer.borderColor = UIColor.hex("#FF0000").cgColor
                break
            case .skip:
                titleLabel.textColor = .hex("#15B100")
                remindLabel.textColor = .hex("#15B100")
                layer.borderColor = UIColor.hex("#15B100").cgColor
                break
            default: break
            }
        } else {
            titleLabel.textColor = .hex("#777777")
            remindLabel.textColor = .hex("#777777")
            layer.borderColor = UIColor.hex("#777777").cgColor
        }
    }

    init(withType type: SuperviseCheckingEnum) {
        super.init(frame: .zero)
        self.type = type
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        layer.borderWidth = 1
        layer.cornerRadius = 6
        
        addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.centerX.equalToSuperview()
        }
        
        addSubview(remindLabel)
        remindLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.centerX.equalToSuperview()
        }
        
        switch type {
        case .pass:
            titleLabel.text = "合格"
            remindLabel.text = "检查通过"
            break
        case .fail:
            titleLabel.text = "不合格"
            remindLabel.text = "检查不通过"
            break
        case .skip:
            titleLabel.text = "不涉及"
            remindLabel.text = "跳过"
            break
        default: break
        }
        updateEntry()
    }
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 17)
        label.textColor = .hex("#333333")
        return label
    }()
    
    lazy var remindLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = .hex("#333333")
        return label
    }()
}

class SuperviseCheckMsgView: UIView {
    let msgTV = BSTextView()
    let imgPicker = BSImagePickerView()

    var images: [UIImage] {
        set {
            imgPicker.images = newValue
        }
        get {
            return imgPicker.images
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .bg
        layer.cornerRadius = 10
        layer.masksToBounds = true

        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        msgTV.placeholder = "请输入附加说明..."
        msgTV.backgroundColor = .white
        msgTV.layer.masksToBounds = true
        msgTV.layer.cornerRadius = 5
        msgTV.layer.borderWidth = 0.5
        msgTV.layer.borderColor = UIColor.hex("#DEDEDE").cgColor
        msgTV.layer.backgroundColor = UIColor.white.cgColor
        msgTV.font = .systemFont(ofSize: 14)
        msgTV.textColor = .hex("#333333")
        msgTV.textContainerInset = UIEdgeInsets(top: 10, left: 8, bottom: 10, right: 10)
        addSubview(msgTV)
        msgTV.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(16)
            make.right.equalToSuperview().offset(-16)
            make.top.equalToSuperview().offset(16)
            make.height.equalTo(63)
        }
        
        imgPicker.isBorder = true
        imgPicker.rowCount = 4
        imgPicker.itemHeight = 65
        imgPicker.backgroundColor = .bg
        imgPicker.key = ""
        imgPicker.collectionView.backgroundColor = .bg
        addSubview(imgPicker)
        imgPicker.snp.makeConstraints { make in
            make.top.equalTo(msgTV.snp.bottom)
            make.left.equalToSuperview().offset(6)
            make.right.equalToSuperview().offset(-6)
            make.bottom.equalTo(self.snp.bottom).offset(-20)
        }
    }
}



